import { WECHAT } from 'cc/env';
import { Databus } from '../core/Databus';
const databus = Databus.Instance();
var event_report_instance = null;
export default class DataEventReport {
  private static instance: DataEventReport = null as unknown as DataEventReport;
  public static Instance() {
    if (DataEventReport.instance) {
      return DataEventReport.instance;
    }
    DataEventReport.instance = new DataEventReport();
    return DataEventReport.instance;
  }
  constructor() {
    if (event_report_instance) {
      return event_report_instance;
    }
    event_report_instance = this;
  }
  // 组件点击上报，页面 -- 组件名（按钮或者弹窗, 加后缀 {sure}/{cancel}/{close})
  public reportComponentEvent(page: string, cn: string) {
    if (WECHAT) {
      wx.reportEvent("client_component_event", {
        "page": page,
        "component_name": cn,
        "gold_coin": databus.selfInfo.goldCoin,
        "play_level": databus.selfInfo.playLevel.playLevel,
        "win_count": databus.selfInfo.winNum,
        "lose_count": databus.selfInfo.loseNum,
        "is_new_user": databus.selfInfo.isNewUser() ? 1 : 0,
      });
    }
  }
  // 核心流程上报 【是否新用户/当前对局次数/游戏胜率】
  public reportCoreProcess(page: string, stage: string, battleCate: number, gameType: number) {
    if (WECHAT) {
      wx.reportEvent("game_core_process", {
        "page": page,
        "game_stage": stage,
        "battle_cate": battleCate,
        "game_type": gameType,
        "play_level": databus.selfInfo.playLevel.playLevel,
        "win_count": databus.selfInfo.winNum,
        "lose_count": databus.selfInfo.loseNum,
        "gold_coin": databus.selfInfo.goldCoin,
        "is_new_user": databus.selfInfo.isNewUser() ? 1 : 0,
      });
    }
  }
  // 核心指标上报 【输赢等】
  public reportCoreMetric(metricName: string, battleCate: number, gameType: number) {
    if (WECHAT) {
      wx.reportEvent("game_core_metrics", {
        "metric_name": metricName,
        "play_level":  databus.selfInfo.playLevel.playLevel,
        "battle_cate": battleCate,
        "game_type": gameType,
        "is_new_user": databus.selfInfo.isNewUser() ? 1 : 0,
      });
    }
  }
  // 启动性能上报
  public reportSceneTime(sceneId: number, costTime: number, scene: string) {
    if (WECHAT) {
      if (!wx.reportScene) { return; }
      wx.reportScene({
        sceneId: sceneId,
        costTime: costTime,
        dimension: {
          scene: scene, // value仅支持传入String类型。若value表示Boolean，请将值处理为'0'、'1'进行上报；若value为Number，请转换为String进行上报
        },
        success (res: any) {
          // 上报接口执行完成后的回调，用于检查上报数据是否符合预期
          console.log('report scene time success ');
        },
        fail (res: any) {
          // 上报报错时的回调，用于查看上报错误的原因：如参数类型错误等
          console.log('report scene time fail ', res);
        }
      });
    }
  }

  // 激励广告广告核心流程上报
  public reportRewardedAdsCoreProcess(adsScene: string, adsStage: number, gameType: number, battleCate: number) {
    if (WECHAT) {
      wx.reportEvent("rewarded_video_core_process", {
        "ads_scene": adsScene,
        "ads_stage": adsStage,
        "gold_coin": databus.selfInfo.goldCoin,
        "is_new_user": databus.selfInfo.isNewUser() ? 1 : 0,
        "win_count": databus.selfInfo.winNum,
        "lose_count": databus.selfInfo.loseNum,
        "game_type": gameType,
        "battle_cate": battleCate,
      });
    }
  }
}